﻿

namespace Mulala.Cavan.Presentation.Mvc.Controller
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Domain.MainBoundedContext.DictionaryModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Presentation.Mvc.ViewModel;
    using Mulala.Cavan.Presentation.Seedwork;
    using Mulala.Cavan.Core.Helper;
    using Mulala.Cavan.Application.Seedwork;

    public class TagController : EntityController<Tag, TagSearch, ITagViewModel, ITagAppService>, ITagController
    {
        private IAreaAppService AreaAppService { get; set; }

        public override string ControllerName
        {
            get { return ControllerConst.TAG; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public TagController()
        {
            this.Model = ModelFactory.CreateModel<ITagViewModel>();
        }

        [ActionName("corporationtags")]
        [ChildActionOnly]
        public ActionResult GetCorporationSearchTags(CorporationSearch corporationSearch)
        {
            ICorporationSearchTagsViewModel corporationSearchTagsViewModel = ModelFactory.CreateModel<ICorporationSearchTagsViewModel>();

            TagSearch search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.CorporationType);
            IList<Tag> corporationTypes = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.CorporationBusinessExperience);
            IList<Tag> corporationBusinessExperiences = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.CorporationScale);
            IList<Tag> corporationScales = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.CorporationSpeciality);
            IList<Tag> corporationSpecialities = this.AppService.GetBySearch(search).ToList();

            corporationSearchTagsViewModel.CorporationBusinessExperience = corporationBusinessExperiences;
            corporationSearchTagsViewModel.CorporationScale = corporationScales;
            corporationSearchTagsViewModel.CorporationSpeciality = corporationSpecialities;
            corporationSearchTagsViewModel.CorporationTypes = corporationTypes;
            AreaSearch areaSearch = AreaSearchFactory.CreateAreaSearch(CavanWebUtility.GetUserSelectedCity(), true);
            corporationSearchTagsViewModel.Towns = this.AreaAppService.GetBySearch(areaSearch).ToList();
            corporationSearchTagsViewModel.URLWithNoCorporationBusinessExperience = GetCorporationSearchUrl(corporationSearch, DonotAppendCorporationSearch.BusinessExperience);
            corporationSearchTagsViewModel.URLWithNoCorporationScale = GetCorporationSearchUrl(corporationSearch, DonotAppendCorporationSearch.Scale);
            corporationSearchTagsViewModel.URLWithNoCorporationSpeciality = GetCorporationSearchUrl(corporationSearch, DonotAppendCorporationSearch.Speciality);
            corporationSearchTagsViewModel.URLWithNoCorporationType = GetCorporationSearchUrl(corporationSearch, DonotAppendCorporationSearch.Type);
            corporationSearchTagsViewModel.SearchParam = corporationSearch;

            return PartialView("~/Views/Tag/_corporationTagsPartial.cshtml", corporationSearchTagsViewModel);
        }

        [ActionName("nannytags")]
        [ChildActionOnly]
        public ActionResult GetNannySearchTags(NannySearch nannySearch)
        {
            INannySearchTagsViewModel nannySearchTagsViewModel = ModelFactory.CreateModel<INannySearchTagsViewModel>();

            TagSearch search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.NannyType);
            IList<Tag> nannyTypes = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.NannyExperience);
            IList<Tag> nannyExperiences = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.NannyAge);
            IList<Tag> nannyAges = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.NannySkill);
            IList<Tag> nannySkills = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.NannyDegree);
            IList<Tag> nannyDegrees = this.AppService.GetBySearch(search).ToList();

            nannySearchTagsViewModel.NannyExperience = nannyExperiences;
            nannySearchTagsViewModel.NannyAge = nannyAges;
            nannySearchTagsViewModel.NannyDegree = nannyDegrees;
            nannySearchTagsViewModel.NannyType = nannyTypes;
            nannySearchTagsViewModel.NannySkill = nannySkills;
            AreaSearch areaSearch = AreaSearchFactory.CreateAreaSearch(CavanWebUtility.GetUserSelectedCity(), true);
            nannySearchTagsViewModel.Towns = this.AreaAppService.GetBySearch(areaSearch).ToList();
            nannySearchTagsViewModel.URLWithNoNannyAge = GetNannySearchUrl(nannySearch, DonotAppendNannySearch.Age);
            nannySearchTagsViewModel.URLWithNoNannyDegree = GetNannySearchUrl(nannySearch, DonotAppendNannySearch.Degree);
            nannySearchTagsViewModel.URLWithNoNannyExperience = GetNannySearchUrl(nannySearch, DonotAppendNannySearch.Experience);
            nannySearchTagsViewModel.URLWithNoNannySkill = GetNannySearchUrl(nannySearch, DonotAppendNannySearch.Skill);
            nannySearchTagsViewModel.URLWithNoNannyType = GetNannySearchUrl(nannySearch, DonotAppendNannySearch.Type);
            nannySearchTagsViewModel.SearchParam = nannySearch;

            return PartialView("~/Views/Tag/_nannyTagsPartial.cshtml", nannySearchTagsViewModel);
        }

        [ActionName("consigntags")]
        [ChildActionOnly]
        public ActionResult GetConsignSearchTags(ConsignSearch consignSearch)
        {
            IConsignSearchTagsViewModel consignSearchTagsViewModel = ModelFactory.CreateModel<IConsignSearchTagsViewModel>();

            TagSearch search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.ConsignType);
            IList<Tag> consignTypes = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.ConsignExperienceNeed);
            IList<Tag> consignExperienceNeeds = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.ConsignAgeNeed);
            IList<Tag> consignAgeNeeds = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.NannySkill);
            IList<Tag> consignSkillNeeds = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.ConsignSalaryProvide);
            IList<Tag> consignSalaryProvides = this.AppService.GetBySearch(search).ToList();
            search = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.ConsignDegreeNeed);
            IList<Tag> consignDegreeNeeds = this.AppService.GetBySearch(search).ToList();

            consignSearchTagsViewModel.ConsignExperienceNeed = consignExperienceNeeds;
            consignSearchTagsViewModel.ConsignAgeNeed = consignAgeNeeds;
            consignSearchTagsViewModel.ConsignSalaryProvide = consignSalaryProvides;
            consignSearchTagsViewModel.ConsignType = consignTypes;
            consignSearchTagsViewModel.ConsignSkillNeed = consignSkillNeeds;
            consignSearchTagsViewModel.ConsignDegreeNeed = consignSkillNeeds;
            AreaSearch areaSearch = AreaSearchFactory.CreateAreaSearch(CavanWebUtility.GetUserSelectedCity(), true);
            consignSearchTagsViewModel.Towns = this.AreaAppService.GetBySearch(areaSearch).ToList();
            consignSearchTagsViewModel.URLWithNoConsignAgeNeed = GetConsignSearchUrl(consignSearch, DonotAppendConsignSearch.Age);
            consignSearchTagsViewModel.URLWithNoConsignDegreeNeed = GetConsignSearchUrl(consignSearch, DonotAppendConsignSearch.Degree);
            consignSearchTagsViewModel.URLWithNoConsignExperienceNeed = GetConsignSearchUrl(consignSearch, DonotAppendConsignSearch.Experience);
            consignSearchTagsViewModel.URLWithNoConsignSalaryProvide = GetConsignSearchUrl(consignSearch, DonotAppendConsignSearch.Salary);
            consignSearchTagsViewModel.URLWithNoConsignSkillNeed = GetConsignSearchUrl(consignSearch, DonotAppendConsignSearch.Skill);
            consignSearchTagsViewModel.URLWithNoConsignType = GetConsignSearchUrl(consignSearch, DonotAppendConsignSearch.Type);
            consignSearchTagsViewModel.SearchParam = consignSearch;

            return PartialView("~/Views/Tag/_consignTagsPartial.cshtml", consignSearchTagsViewModel);
        }

        private string GetCorporationSearchUrl(CorporationSearch corporationSearch, DonotAppendCorporationSearch donotAppend)
        {
            bool hasCondition = false;
            StringBuilder url = new StringBuilder(Project.Settings.HostServer);
            if (corporationSearch.AreaSpell.IsNotEmpty())
            {
                if (donotAppend != DonotAppendCorporationSearch.Area)
                {
                    url.Append("/" + corporationSearch.AreaSpell);
                }
                else
                {
                    url.Append("/{0}");
                }
            }
            url.Append("/" + ControllerConst.CORPORATION.ToLower());
            url.Append("/" + ActionConst.LIST);
            if (corporationSearch.CurrentPage > 0)
            {
                url.Append("/cp" + corporationSearch.CurrentPage.ToString() + "_");
            }
            if (corporationSearch.BusinessExperience > 0)
            {
                if (donotAppend != DonotAppendCorporationSearch.BusinessExperience)
                {
                    hasCondition = true;
                    url.Append("e" + corporationSearch.BusinessExperience.ToString() + "-");
                }
            }
            if (corporationSearch.Scale > 0)
            {
                if (donotAppend != DonotAppendCorporationSearch.Scale)
                {
                    hasCondition = true;
                    url.Append("s" + corporationSearch.Scale.ToString() + "-");
                }
            }
            if (corporationSearch.Type.HasValue)
            {
                if (donotAppend != DonotAppendCorporationSearch.Type)
                {
                    hasCondition = true;
                    url.Append("t" + corporationSearch.Type.Value.GetHashCode().ToString() + "-");
                }
            }
            if (corporationSearch.SkillPosition.Count() > 0)
            {
                if (donotAppend != DonotAppendCorporationSearch.Speciality)
                {
                    hasCondition = true;
                    url.Append("sp" + corporationSearch.SkillPosition[0].ToString() + "-");
                }
            }
            if (corporationSearch.OrderBy.Count > 0)
            {

                bool desc = false;
                if (corporationSearch.OrderBy.ContainsKey("ViewTotal"))
                {
                    hasCondition = true;
                    corporationSearch.OrderBy.TryGetValue("ViewTotal", out desc);
                    if (desc)
                    {
                        url.Append("ob1");
                    }
                    else
                    {
                        url.Append("ob2");
                    }
                }
                else if (corporationSearch.OrderBy.ContainsKey("BusinessExperience"))
                {
                    hasCondition = true;
                    corporationSearch.OrderBy.TryGetValue("BusinessExperience", out desc);
                    if (desc)
                    {
                        url.Append("ob3");
                    }
                    else
                    {
                        url.Append("ob4");
                    }
                }
            }
            if (hasCondition)
            {
                url.Append("-");
            }
            return url.ToString();
        }

        private string GetNannySearchUrl(NannySearch nannySearch, DonotAppendNannySearch donotAppend)
        {
            bool hasCondition = false;
            StringBuilder url = new StringBuilder(Project.Settings.HostServer);
            if (nannySearch.AreaSpell.IsNotEmpty())
            {
                if (donotAppend != DonotAppendNannySearch.Area)
                {
                    url.Append("/" + nannySearch.AreaSpell);
                }
                else
                {
                    url.Append("/{0}");
                }
            }
            url.Append("/" + ControllerConst.NANNY.ToLower());
            url.Append("/" + ActionConst.LIST);
            if (nannySearch.CurrentPage > 0)
            {
                url.Append("/cp" + nannySearch.CurrentPage.ToString() + "-");
            }
            if (nannySearch.Experience > 0)
            {
                if (donotAppend != DonotAppendNannySearch.Experience)
                {
                    hasCondition = true;
                    url.Append("e" + nannySearch.Experience.ToString() + "-");
                }
            }
            if (nannySearch.Age > 0)
            {
                if (donotAppend != DonotAppendNannySearch.Age)
                {
                    hasCondition = true;
                    url.Append("a" + nannySearch.Age.ToString() + "-");
                }
            }
            if (nannySearch.Type.HasValue)
            {
                if (donotAppend != DonotAppendNannySearch.Type)
                {
                    hasCondition = true;
                    url.Append("t" + nannySearch.Type.Value.GetHashCode().ToString() + "-");
                }
            }
            if (nannySearch.SkillPosition.Count() > 0)
            {
                if (donotAppend != DonotAppendNannySearch.Skill)
                {
                    hasCondition = true;
                    url.Append("sp" + nannySearch.SkillPosition[0].ToString() + "-");
                }
            }
            if (nannySearch.IsMarried.HasValue)
            {
                if (donotAppend != DonotAppendNannySearch.IsMarried)
                {
                    hasCondition = true;
                    string isMarried = (nannySearch.IsMarried.Value) ? "1" : "0";
                    url.Append("im" + isMarried + "-");
                }
            }
            if (nannySearch.Degree > 0)
            {
                if (donotAppend != DonotAppendNannySearch.Degree)
                {
                    hasCondition = true;
                    url.Append("d" + nannySearch.Degree.ToString() + "-");
                }
            }
            if (nannySearch.OrderBy.Count > 0)
            {
                bool desc = false;
                if (nannySearch.OrderBy.ContainsKey("ViewTotal"))
                {
                    hasCondition = true;
                    nannySearch.OrderBy.TryGetValue("ViewTotal", out desc);
                    if (desc)
                    {
                        url.Append("ob1");
                    }
                    else
                    {
                        url.Append("ob2");
                    }
                }
                else if (nannySearch.OrderBy.ContainsKey("Experience"))
                {
                    hasCondition = true;
                    nannySearch.OrderBy.TryGetValue("Experience", out desc);
                    if (desc)
                    {
                        url.Append("ob3");
                    }
                    else
                    {
                        url.Append("ob4");
                    }
                }
            }
            if (hasCondition)
            {
                url.Append("-");
            }
            return url.ToString();
        }

        private string GetConsignSearchUrl(ConsignSearch consignSearch, DonotAppendConsignSearch donotAppend)
        {
            bool hasCondition = false;
            StringBuilder url = new StringBuilder(Project.Settings.HostServer);
            if (consignSearch.AreaSpell.IsNotEmpty())
            {
                if (donotAppend != DonotAppendConsignSearch.Area)
                {
                    url.Append("/" + consignSearch.AreaSpell);
                }
                else
                {
                    url.Append("/{0}");
                }
            }
            url.Append("/" + ControllerConst.NANNY.ToLower());
            url.Append("/" + ActionConst.LIST);
            if (consignSearch.CurrentPage > 0)
            {
                url.Append("/cp" + consignSearch.CurrentPage.ToString() + "-");
            }
            if (consignSearch.Experience > 0)
            {
                if (donotAppend != DonotAppendConsignSearch.Experience)
                {
                    hasCondition = true;
                    url.Append("e" + consignSearch.Experience.ToString() + "-");
                }
            }
            if (consignSearch.Age > 0)
            {
                if (donotAppend != DonotAppendConsignSearch.Age)
                {
                    hasCondition = true;
                    url.Append("a" + consignSearch.Age.ToString() + "-");
                }
            }
            if (consignSearch.Type.HasValue)
            {
                if (donotAppend != DonotAppendConsignSearch.Type)
                {
                    hasCondition = true;
                    url.Append("t" + consignSearch.Type.Value.GetHashCode().ToString() + "-");
                }
            }
            if (consignSearch.SkillPosition.Count() > 0)
            {
                if (donotAppend != DonotAppendConsignSearch.Skill)
                {
                    hasCondition = true;
                    url.Append("sp" + consignSearch.SkillPosition[0].ToString() + "-");
                }
            }
            if (consignSearch.Salary > 0)
            {
                if (donotAppend != DonotAppendConsignSearch.Salary)
                {
                    hasCondition = true;
                    url.Append("s" + consignSearch.Age.ToString() + "-");
                }
            }
            if (consignSearch.Degree > 0)
            {
                if (donotAppend != DonotAppendConsignSearch.Degree)
                {
                    hasCondition = true;
                    url.Append("d" + consignSearch.Degree.ToString() + "-");
                }
            }
            if (consignSearch.OrderBy.Count > 0)
            {
                bool desc = false;
                if (consignSearch.OrderBy.ContainsKey("ViewTotal"))
                {
                    hasCondition = true;
                    consignSearch.OrderBy.TryGetValue("ViewTotal", out desc);
                    if (desc)
                    {
                        url.Append("ob1");
                    }
                    else
                    {
                        url.Append("ob2");
                    }
                }
                else if (consignSearch.OrderBy.ContainsKey("Salary"))
                {
                    hasCondition = true;
                    consignSearch.OrderBy.TryGetValue("Salary", out desc);
                    if (desc)
                    {
                        url.Append("ob3");
                    }
                    else
                    {
                        url.Append("ob4");
                    }
                }
            }
            if (hasCondition)
            {
                url.Append("-");
            }
            return url.ToString();
        }

        private enum DonotAppendCorporationSearch
        {
            BusinessExperience,
            Scale,
            Type,
            Speciality,
            Area
        }

        private enum DonotAppendNannySearch
        {
            Experience,
            Age,
            Type,
            Skill,
            Degree,
            IsMarried,
            Area
        }

        private enum DonotAppendConsignSearch
        {
            Experience,
            Age,
            Type,
            Skill,
            Degree,
            Salary,
            Area
        }
    }
}
